package com.corn.flink.lesson2;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2023/2/22 11:14
 */

public class FlinkSourceFromKafkaDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // flink 从 kafka获取数据 官方提供了kafka的connector供用户直接使用 引入执行版本jar包开箱即用
        // https://nightlies.apache.org/flink/flink-docs-release-1.16/docs/connectors/datastream/kafka/
        String brokers = "localhost:32632";
        String topic = "flink-kafka";
        String groupId = "flink-kafka-test-group";
        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers(brokers)
                .setTopics(topic)
                .setGroupId(groupId)
                // 设置消息消费的offset earliest从消息开始位置 或者 latest最近一条
                .setStartingOffsets(OffsetsInitializer.earliest())
                // 设置value 的 反序列化器
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();
        DataStreamSource<String> kafkaDS = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source");
        kafkaDS.print("kafka source print");
        env.execute();
    }
}
